package com.yonyou.base.flower.impl;

import com.yonyou.base.flower.dao.IFlowerStorageDao;
import com.yonyou.base.flower.model.Flower;
import com.yonyou.base.flower.service.IFlowerStorageService;
import com.yonyou.hrcloud.adder.model.param.Limit;
import com.yonyou.hrcloud.adder.model.param.PageImpl;
import com.yonyou.hrcloud.adder.model.param.PagerRequest;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Service
@Transactional
public class FlowerStorageServiceImpl implements IFlowerStorageService {

    @Resource
    private IFlowerStorageDao flowerStorageDao;

    @Override
    public void deleteFlower(String flowerId) {
        flowerStorageDao.deleteFlower(flowerId);
    }

    @Override
    public Page<Flower> queryFlower(String userId, Integer page, Integer size) {
        List<Flower> flowerList = flowerStorageDao.queryFlower(userId, new Limit(page * size, size));
        Long count = flowerStorageDao.queryFlowerCount(userId);
        return new PageImpl<>(flowerList, new PagerRequest(page, size), count);
    }

    @Override
    public void insertFlower(Flower flower) {
        flowerStorageDao.insertFlower(flower);
    }

    @Override
    public void updateFlower(Flower flower) {
        flowerStorageDao.updateFlower(flower);
    }

    @Override
    public Flower queryHomeFlower() {
        Long count = flowerStorageDao.queryFlowerCount(null);
        if (count == 0) {
            return null;
        }
        int page = new Random().nextInt(flowerStorageDao.queryFlowerCount(null).intValue());
        return flowerStorageDao.queryFlower(null, new Limit(page * 1, 1)).get(0);
    }
}
